<template>
  <div>
    <!-- 1.定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>医院荣誉</el-breadcrumb-item>
      <el-breadcrumb-item>荣誉信息</el-breadcrumb-item>
    </el-breadcrumb>


<el-card class="box-card" >
  <el-row :gutter="20">
    <el-col :span="12">
      <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable  @clear="getList()"> <!-- //修改 20:10 -->
          <el-button slot="append" icon="el-icon-search" @click="getList()"></el-button> <!-- //修改 20:10 -->
      </el-input>
      </el-col>
    <el-col :span="6">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/addHonor' }">
         <el-button type="primary" >添加信息</el-button>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

        <!-- :data 就是 v-for prop就是要绑定的数据-->
       <!-- 用户数据表格展现  border 边框线属性
              type="index" 表格的索引值
       -->
       <el-table :data="patientList" border :row-class-name="tableRowClassName">
           <el-table-column type="index" label="序号"></el-table-column>
           <el-table-column prop="honorname" label="医院名称"></el-table-column>
           <el-table-column prop="honormessage" label="医院荣誉"></el-table-column>
           <el-table-column prop="honortime" label="荣获时间"></el-table-column>

           <el-table-column label="操作">
             <template slot-scope="scope">

                <el-button type="primary" icon="el-icon-edit" size="small" @click="updataHonorBtn(scope.row)"></el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" @click="deletePatient(scope.row)"></el-button>
             </template>
           </el-table-column>
       </el-table>
   </el-card>
    <el-pagination
         @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
         :current-page="queryInfo.pageNum"
         :page-sizes="[5, 10, 20,40]"
         :page-size="queryInfo.pageSize"
         layout="total, sizes, prev, pager, next, jumper"
         :total="total">
    </el-pagination>


<!-- 修改信息对话框-->
    <el-dialog title="修改信息" :visible.sync="updataPatientVisible" width="50%" @close="closeAddPatientDialog" >

      <el-form :model="updataHonor" :rules="rules"  ref="itemPatientForm" label-width="100px" >
        <el-form-item label="医院名称:" prop="honorname" >

          <el-input v-model="updataHonor.honorname" ></el-input>
        </el-form-item>
        <el-form-item label="医院荣誉:" prop="honormessage">


          <el-input v-model="updataHonor.honormessage" ></el-input>


        </el-form-item>

           <el-form-item label="荣获时间">
               <el-col :span="11">
                 <el-date-picker type="date" placeholder="选择日期" v-model="updataHonor.honortime" style="width: 100%;"></el-date-picker>
               </el-col>
             </el-form-item>



      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updataPatientVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataHonor1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default{
    data(){
      const checkPhone = (rule, value, callback) => {

        //定义校验手机号的正则语法
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test (value)) {

          return callback()
        }
        callback(new Error('请填写正确的手机号'))

      }
      return{
        // addPatientVisible: false,
        updataPatientVisible: false,
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 5
        },
        updataHonor: {
        },
        total: 0,
        patientList: [],
        itemPatientForm:{},
        updataPatientForm:{},
        rules:{
            username:[
              /* required是否为必填项 message:提示信息 trigger触发条件*/
              {required: true,message: "请输入姓名",trigger: 'blur'}
            ],
            phone:[
              { required: true, message: '请输入时间', trigger: 'blur' },
              { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
              { validator: checkPhone , trigger: 'blur' }
            ],
        },
        props: {
          //定义子节点菜单展开方式
          expandTrigger: "click",
          value: "id", //选中数据的value值
          label: "name", //选中数据展现名称
          children: "children", //自选项数据

          // checkStrictly: true
        },
        parentHospitalList: [],
        DoctorForm: {
          name: '', //定义商品分类名称
          parentId: 0, //默认父级ID=0
          level: 1 //默认是一级菜单
        },
        DoctorList:[],
        value:[],
        selectedKeys:[]
    }
   },
    methods:{

      // async getHospitalList(){
      //   const {data:result} = await this.$http.get(`/honor/updataHonor`)
      //   if (result.status !== 200) return this.$message.error("获取荣誉失败!!")
      //   this.parentHospitalList = result.data
      // },
      // parentHospitalChange() {
      //   this.updataPatientForm.hospital = this.$refs["refSubCat"].getCheckedNodes()[0].label
      //   console.log(this.updataPatientForm.hospital)
      //   // this.itemPatientForm.hospital = this.$refs["refSubCat"].getCheckedNodes()[0].label
      // },
      async deletePatient(Patient){
        const result =  await this.$confirm('此操作将永久删除 , 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                          }).catch(error => error)
                  //如果确认  confirm  如果取消 cancel
          if(result !== 'confirm'){
              return this.$message.info("删除取消")
         }

         const {data:result1} = await this.$http.delete(`/honor/delete/${Patient.id}`)
         this.$message.success("删除成功")
        this.getList() //修改20:09
      },
      //修改15:51
      // updataHonorBtn(Honor){
      //   this.getHonorList()
      //   this.updataHonorForm = Honor;
      //   this.updataHonorVisible = true;

      // },
      //修改 16:10
      async updataHonorBtn(honor){
        this.updataPatientVisible = true
        const {data: result} = await this.$http.get("/honor/"+honor.id)
        if(result.status !== 200) return this.$message.error("查询失败")
        this.updataHonor = result.data
      },
      //修改16:38
      closeUpdateDialog(){
        //重置表格数据
        this.$refs.updateHonorRef.resetFields()
      },
      async updataHonor1(){
        let honor = {}
        honor.id = this.updataHonor.id
        honor.honorname= this.updataHonor.honorname
        honor.honormessage = this.updataHonor.honormessage
        honor.honortime = this.updataHonor.honortime
          const { data: result } = await this.$http.put("/honor/updataHonor",honor)
          if(result.status !== 200) return this.$message.error("修改失败")
          if(result.data === 'no1') return this.$message.error("修改失败,新密码与旧密码相同")
          this.$message.success('修改成功');
          this.updataPatientVisible = false
          this.getList() //修改20:14
      },
      async getList(){  //修改20:15
        const {data: result} = await this.$http.get('/honor/honorList',{
           params: this.queryInfo
        })
        if(result.status !== 200) return this.$message.error("荣誉列表查询失败")
        this.patientList = result.data.rows
        this.total = result.data.total
      },

     async addHonor(){
        this.$refs.itemPatientFormRef.validate(async valid => {
          //如果校验失败 则停止数据
          if(!valid) return
          const{data: result} = await this.$http.post(`/honor/addHonor`,this.addHonor)
           if(result.status !== 200) return this.$message.error("数据添加失败")
           this.getList();  //修改20:11
           this.addHonor = false;
          this.addHonor = {}
        })
      },
      // addPatientBtn(){
      //   this.getHospitalList();
      //   this.addPatientVisible = true
      // },
      handleSizeChange(pageSize){

        this.queryInfo.pageSize = pageSize
        this.getList()
      },
      handleCurrentChange(pageNum){

        this.queryInfo.pageNum = pageNum
        this.getList()  //修改20:12
      },
      //当点击关闭按钮时,应该重置整个表单
      closeAddPatientDialog() {
        this.itemPatientForm = {}
      },
      tableRowClassName({row, rowIndex}) {
              if (rowIndex === 1) {
                return 'warning-row';
              } else if (rowIndex === 3) {
                return 'success-row';
              }
              return '';
            }
    },
    mounted(){
      this.getList() //20:12
    }
  }
</script>

<style>
  input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
          -webkit-appearance: none;
      }
      input[type="number"]{
          -moz-appearance: textfield;
      }
.el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

</style>
